package com.pinyoumei.app.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.CouponAdapter;
import com.pinyoumei.app.bean.CouponBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-06-26.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-06-26        Administrator             新增Person类
 * 2021-06-26        Administrator             增加sex属性
 * 优惠券列表
 */
public class CouponListActivity extends BaseActivity implements View.OnClickListener {

    private final static String[] TITLE = {"全部","待使用","已使用","已过期"};
    private TabLayout tabPosTab;
    private int page = 1;
    private int coupon_status = -1;
    private CouponAdapter couponAdapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_list);
        InitWidget();


    }

    @Override
    protected void onResume() {
        super.onResume();
        InitData();
    }

    private void InitWidget() {
        recyclerView = findViewById(R.id.recyclerView);
        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);
        tabPosTab = findViewById(R.id.tab_pos_tab);
//        TextView tv_make_rover_type = findViewById(R.id.tv_make_rover_type);
//        tv_make_rover_type.setOnClickListener(this);



        SmartRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        //首页数据下拉刷新监听
        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                InitData();
                refreshLayout.finishRefresh();

            }
        });
        swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                LoadData();
                refreshLayout.finishLoadMore();
            }
        });


        for (int i = 0; i < TITLE.length; i++) {
            tabPosTab.addTab(tabPosTab.newTab().setText(TITLE[i]));
        }

        tabPosTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int tabPosition = tab.getPosition();
                page = 1;
                switch (tabPosition) {
                	case 0:
                        coupon_status = -1;
                        InitData();
                		break;

                    case 1:
                        coupon_status = 0;
                        InitData();
                        break;
                    case 2:
                        coupon_status = 1;
                        InitData();
                        break;
                    case 3:
                        coupon_status = 2;
                        InitData();
                        break;
                	default:
                		break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        couponAdapter = new CouponAdapter(R.layout.item_coupon_list,null);
        couponAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(couponAdapter);
        //空页面
        View notDataView = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) recyclerView.getParent(), false);
        couponAdapter.setEmptyView(notDataView);

//        couponAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (couponAdapter.getData().get(position).getCoupon_status()==0){
//                    switch (couponAdapter.getData().get(position).getCoupon_type()) {
////                        优惠券类型 1机具商城优惠券 2境外商城优惠券
//                    	case 1:
//                    		startActivity(new Intent(CouponListActivity.this, GetPOSDevicesActivity.class));
//                    		break;
//                        case 2:
//                            startActivity(new Intent(CouponListActivity.this, GetPOSDevicesActivity.class));
//                            break;
//                    	default:
//                    		break;
//                    }
//                }
//            }
//        });

    }






    private void InitData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(CouponListActivity.this));
        map.put("coupon_status", coupon_status);
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.ZBK_OUTLAND_COUPON)
                .params("user_id", SPUtils.getAppUserId(CouponListActivity.this))
                .params("coupon_status", coupon_status)
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        CouponBean couponBean = gson.fromJson(response.body(), CouponBean.class);
                        if (couponBean.getCode() >= 0) {
                            couponAdapter.setNewData(couponBean.getResult().getCoupon_list());
                            View notDataView2 = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) recyclerView.getParent(), false);
                            couponAdapter.setEmptyView(notDataView2);
                        } else {
                            ToastUtils.showShort(couponBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }

    private void LoadData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(CouponListActivity.this));
        map.put("coupon_status", coupon_status);
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.ZBK_OUTLAND_COUPON)
                .params("user_id", SPUtils.getAppUserId(CouponListActivity.this))
                .params("coupon_status", coupon_status)
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        CouponBean couponBean = gson.fromJson(response.body(), CouponBean.class);
                        if (couponBean.getCode() >= 0) {
                            couponAdapter.addData(couponBean.getResult().getCoupon_list());

                        } else {
                            ToastUtils.showShort(couponBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish:
        		finish();
        		break;

        	default:
        		break;
        }
    }
}
